import { useModel } from 'umi';

export default function useAuthModel() {
  const { initialState } = useModel('@@initialState');

  const hasRight = (key: string) => {
    if (!key || key == '') {
      return false;
    }

    const { currentUser } = initialState || {};

    // console.log('key: ', key);
    // console.log('currentUser: ', currentUser);

    if (!currentUser) {
      return false;
    }

    if (currentUser.super) {
      return true;
    }

    const userFuncRights: string[] = currentUser.funcRights || [];
    return userFuncRights.indexOf(key) !== -1;
  };

  return {
    hasRight,
  };
}
